#!/usr/bin/php
<?php
include('/root/tv_record/xmltv/settings.php.inc');
include('/root/tv_record/xmltv/sql.php.inc');


$sql = "SELECT * from recording_shows";
$result10 = mysql_query($sql,$connect);

while ($r =mysql_fetch_array($result10)) {
$title = $r[1];
$call_sign = $r[4];
$channel = $r[3];
$nottvdb = $r[5];
//find upcoming shows and add them so we record them
$sql = "select * from listings where title=\"$title\" and previously_shown =\"\" and channel=\"$call_sign\"" ;
$result = mysql_query($sql,$connect) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
        $start = strtotime($row[1]);
        $newstart = strtotime('-1 minute',$start);
        $start = date("Y-m-d H:i:s",$newstart);

        $end = strtotime($row[2]);
        $newend = strtotime('+3 minutes',$end);
        $end = date("Y-m-d H:i:s",$newend);
        $title = $row[4];
	$title_no_change = $title;
        $channel = $channel;
        $episode_num_onscreen = $row[9];
        $sub_title=$row[12];
	$episode_num_dd_progid=$row[8];


        $guess_season=00;
        $guess_episode=00;

        if ($episode_num_onscreen != "") {
		if ($title_no_change == "House Hunters") {
                	$guess_season = substr($episode_num_onscreen,0,2);
                	$guess_episode = substr($episode_num_onscreen,2);
		} else {
                	$guess_season = substr($episode_num_onscreen,0,1);
                	$guess_episode = substr($episode_num_onscreen,1);
		}
        }
        $ep = preg_replace("/[^a-zA-Z0-9\s]/", "", $title);
        if ($sub_title == "" && $title_no_change == "House Hunters" || $sub_title == "" && $title_no_change == "Yard Crashers") {
                $sub_title = "Episode " . intval($guess_episode);
        }

        if (strlen($guess_episode) == 1) {
                $guess_episode="0".$guess_episode;
        }
        if (strlen($guess_season) >=2) {
                $ep .= ".S".$guess_season."E".$guess_episode;
        } else {
                $ep .= ".S0".$guess_season."E".$guess_episode;
        }

        #if ($sub_title == "" && $title_no_change == "House Hunters") {
        #        $sub_title = "Episode $guess_episode";
        #}
        #if (strlen($guess_episode) == 1) {
        #        $guess_episode="0".$guess_episode;
        #}
        #$ep .= ".S0".$guess_season."E".$guess_episode;

	$sub_title = str_replace("&","and",$sub_title);
        $sub_title = preg_replace("/[^a-zA-Z0-9\s]/", "", $sub_title);
        $ep .= ".".$sub_title;
        $ep = str_replace(" ",".",$ep);
        //echo $ep ." $start $end\n";
	$sql = "SELECT count(*) from torecord where episode_num_dd_progid=\"$episode_num_dd_progid\" and recording_title =\"$title_no_change\"";
	$result3 = mysql_query($sql,$connect) or die (mysql_error());
	$row2 = mysql_fetch_row($result3);
	if ($row2[0] == 0) {
        	$sql = "INSERT into torecord (start,end,filename,channel,recording_title,recording_subtitle,episode_num_dd_progid,notvdb) values (\"$start\",\"$end\",\"$ep\",\"$channel\",\"$title\",\"$sub_title\",\"$episode_num_dd_progid\",\"$nottvdb\")";
		$result2 = mysql_query($sql,$connect);
	} else {
		$sql = "SELECT manual from torecord where episode_num_dd_progid=\"$episode_num_dd_progid\" and recording_title=\"$title\"";
		$result2 = mysql_query($sql,$connect);
		$row7=mysql_fetch_row($result2);
		$manual = $row7[0];
		if ($manual == 1) {
			$sql = "UPDATE torecord set end=\"$end\",recording_subtitle=\"$sub_title\" where episode_num_dd_progid=\"$episode_num_dd_progid\",notvdb=\"$nottvdb\" and recording_title=\"$title\"";
			$result2 = mysql_query($sql,$connect);
		} else {
			$sql = "UPDATE torecord set start=\"$start\",end=\"$end\",recording_subtitle=\"$sub_title\",filename=\"$ep\",notvdb=\"$nottvdb\" where episode_num_dd_progid=\"$episode_num_dd_progid\" and recording_title=\"$title\"";
			$result2 = mysql_query($sql,$connect);
		}
	}

}
}
?>
